package com.fa.production.controller;

import com.alibaba.excel.EasyExcel;
import com.fa.common.core.domain.AjaxResult;
import com.fa.common.core.page.TableDataInfo;
import com.fa.production.domain.*;
import com.fa.production.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import com.fa.common.annotation.Anonymous;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.fa.common.core.controller.BaseController;

import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import static com.fa.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/system/process")
public class ProcessController extends BaseController {
    @Autowired
    private ProcessService processService;

    /**
     * 获取所有流程
     *
     * @return
     */
    @RequestMapping("/list")
    public TableDataInfo process(Processs process) {
        startPage();
        List<Processs> list = processService.getlist(process);
        return getDataTable(list);
    }


    // 根据id获取流程信息
    @GetMapping(value = "/{routeId}")
    @Anonymous
    public AjaxResult getInfo(@PathVariable Integer routeId) {
        return success(processService.selectProcesssById(routeId));
    }
    @GetMapping(value = "/routeProcess/{recordId}")
    @Anonymous
    public AjaxResult getInfo1(@PathVariable Integer recordId) {
        return success(processService.selectRouteProcessById(recordId));
    }
    @GetMapping(value = "/routeProduct/{recordId}")
    @Anonymous
    public AjaxResult getInfo2(@PathVariable Integer recordId) {
        return success(processService.selectRouteProductById(recordId));
    }

    // 新增流程
    @PostMapping()
    public AjaxResult add(@Validated @RequestBody Processs process) {
//        if (!postService.checkPostNameUnique(process))
//        {
//            return error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
//        }
//        else if (!postService.checkPostCodeUnique(post))
//        {
//            return error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
//        }
        return toAjax(processService.insertProcesss(process));
    }
    @PostMapping("/routeprocess")
    public AjaxResult adds(@Validated @RequestBody ProRouteProcess proRouteProcess) {
        return toAjax(processService.insertRouteProcesss(proRouteProcess));
    }
    @PostMapping("/addRouteProduct")
    public AjaxResult addRouteProduct(@Validated @RequestBody ProRouteProduct proRouteProduct) {
        return toAjax(processService.insertRouteProduct(proRouteProduct));
    }


    // 删除流程
    @DeleteMapping(value = "/{routeId}")
    public AjaxResult remove(@PathVariable List<Integer> routeId) {
        return toAjax(processService.deleteProcesssById(routeId));
    }
    @DeleteMapping(value = "/routeProcess/{recordId}")
    public AjaxResult remove1(@PathVariable List<Integer> recordId) {
        return toAjax(processService.deleteRouteProcesssById(recordId));
    }
    @DeleteMapping(value = "/routeProduct/{recordId}")
    public AjaxResult remove2(@PathVariable List<Integer> recordId) {
        return toAjax(processService.deleteRouteProductById(recordId));
    }

    // 修改流程
    @PutMapping()
    public AjaxResult edit(@Validated @RequestBody Processs process) {
        return toAjax(processService.updateProcesss(process));
    }
    @PutMapping("/routeprocess")
    public AjaxResult edit1( @RequestBody ProRouteProcess proRouteProcess) {
        return toAjax(processService.updateRouteProcesss(proRouteProcess));
    }
    @PutMapping("/routeproduct")
    public AjaxResult edit2( @RequestBody ProRouteProduct proRouteProduct) {
        return toAjax(processService.updateRouteProduct(proRouteProduct));
    }

    //查询组成工序
    @GetMapping("/getList1")
    public AjaxResult getList(Long routeId) {
        return success(processService.getlist1(routeId));
    }
    //查询关联产品
    @GetMapping("/getList2")
    public AjaxResult getList2(Integer routeId) {
        return success(processService.getlist2(routeId));
    }
    @GetMapping("/getMdItemTypeTreeList")
    public AjaxResult getMdItemTypeTreeList() {
        return success(processService.getMdItemTypeTreeList());
    }
    @GetMapping("/getMdItemList")
    public AjaxResult getMdItemList(MdItem mdItem) {
        return success(processService.getMdItemList(mdItem));
    }
    //通过id查询物料产品
    @GetMapping("/getMdItemByIdList")
    public AjaxResult getMdItemByIdList(@RequestParam ArrayList<Long> allIds) {
        return success(processService.getMdItemByIdList(allIds));
    }
    @PostMapping("/export")
    public void exportUnit(HttpServletResponse response) throws Exception {
        System.out.println("开始导出----------------------------------");

        List<Processs> list =processService.getlist(new Processs());

        // 导出.xlsx用这个
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("工艺流程", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        EasyExcel.write(response.getOutputStream(), Processs.class).sheet("工艺流程列表").doWrite(list);
    }
}
